package com.lzh.persevere.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lzh.persevere.common.BaseEntity;
import com.lzh.persevere.mapper.EssayInfoMapper;
import com.lzh.persevere.model.entity.EssayInfo;
import com.lzh.persevere.model.entity.FileInfo;
import com.lzh.persevere.model.qo.essay.EssayPageQO;
import com.lzh.persevere.model.ro.essay.SendEssayRO;
import com.lzh.persevere.model.vo.essay.EssayVO;
import com.lzh.persevere.service.EssayInfoService;
import com.lzh.persevere.template.PageAbstractTemplate;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.*;
import java.util.stream.Collectors;

/**
 * 随笔表服务实现类
 *
 * @author luzhiheng
 * @since 2025-07-26
 */
@Service
public class EssayInfoServiceImpl extends ServiceImpl<EssayInfoMapper, EssayInfo> implements EssayInfoService {

    @Resource
    private RedisCacheService redisCacheService;

    @Override
    public Map<String, Object> pageList(EssayPageQO qo) {
        Map<String, Object> otherQueryMap = Collections.emptyMap();
        PageAbstractTemplate template = new PageAbstractTemplate() {
            @Override
            public Integer countTotal() {
                return baseMapper.countList(this.getQueryMap(qo.getPageModel(), otherQueryMap));
            }

            @Override
            public List<Map<String, Object>> queryPageList() {
                List<Map<String, Object>> pagedList = baseMapper.pageList(this.queryMap);
                Map<String, String> allUserIdAvatarMap = redisCacheService.getAllUserIdAvatarMap();
                Map<String, String> allUserIdNickNameMap = redisCacheService.getAllUserIdNickNameMap();
                for (int i = 0; i < pagedList.size(); i++) {
                    Map<String, Object> map = pagedList.get(i);
                    map.put("createNickName", allUserIdNickNameMap.get(String.valueOf(map.get("createUserId"))));
                    map.put("avatar", allUserIdAvatarMap.get(String.valueOf(map.get("createUserId"))));
                }
                return pagedList;
            }
        };
        return template.queryPage();
    }

    @Override
    public EssayVO sendEssay(SendEssayRO ro) {
        EssayInfo essayInfo = new EssayInfo().setContent(ro.getContent());
        baseMapper.insert(essayInfo);
        EssayVO essayVO = new EssayVO();
        essayVO.setId(essayInfo.getId());
        essayVO.setCreateTime(essayInfo.getCreateTime());
        essayVO.setCreateUserId(essayInfo.getCreateUserId());
        essayVO.setContent(essayInfo.getContent());

        Map<String, String> allUserIdAvatarMap = redisCacheService.getAllUserIdAvatarMap();
        Map<String, String> allUserIdNickNameMap = redisCacheService.getAllUserIdNickNameMap();
        essayVO.setCreateNickName(allUserIdNickNameMap.get(essayVO.getCreateUserId()));
        essayVO.setAvatar(allUserIdAvatarMap.get(essayVO.getCreateUserId()));
        return essayVO;
    }
}
